Cosmo-Tech SonarQube Setup

This project contains Ansible playbooks and configuration files to set up and manage the SonarQube server with PostgreSQL for Cosmo-Tech.

Project Structure

.
├── ansible.cfg           # Ansible configuration
├── group_vars/           # Variables shared across playbooks
│   └── all.yml           # Common variables for all environments
├── hosts.ini             # Inventory file for production
├── inventories/          # Environment-specific inventories
│   └── dev/              # Development environment
│       ├── hosts         # Development hosts file
│       └── group_vars/   # Development-specific variables
│           └── all.yml   # Only contains overrides from group_vars/all.yml
├── playbooks/            # Playbook directory
│   └── main.yml          # Main playbook that includes roles
├── roles/                # Roles directory
│   ├── docker/           # Docker role
│   │   ├── defaults/     # Default variables for Docker
│   │   │   └── main.yml
│   │   └── tasks/        # Tasks for Docker installation
│   │       └── main.yml
│   ├── postgresql/       # PostgreSQL role
│   │   ├── defaults/     # Default variables for PostgreSQL
│   │   │   └── main.yml
│   │   └── tasks/        # Tasks for PostgreSQL installation
│   │       └── main.yml
│   └── sonarqube/        # SonarQube role
│       ├── defaults/     # Default variables for SonarQube
│       │   └── main.yml
│       ├── meta/         # Role metadata
│       │   └── main.yml
│       ├── tasks/        # Tasks for SonarQube installation
│       │   └── main.yml
│       └── templates/    # Templates for SonarQube
│           └── docker-compose.yml.j2  # Docker Compose template
├── tests/                # Test directory
│   ├── integration/      # Integration tests
│   │   └── test_sonarqube.py
│   ├── conftest.py       # Pytest configuration
│   ├── run_tests.py      # Test runner script
│   └── syntax_check.yml  # Syntax and linting checks
└── Vagrantfile           # Vagrant configuration for local development

Prerequisites

For Production Deployment

For Local Development

Setup

  1. Create and activate a Python virtual environment:
uv venv
source .venv/bin/activate
  1. Install project dependencies:
# Core dependencies
uv pip install .

# Testing dependencies
pip install -r requirements.txt
  1. Install system dependencies (for local development):
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y virtualbox vagrant docker.io

# Enable Docker
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER  # Log out and back in after this
  1. For production, set the required environment variable for the PostgreSQL password:
export SONARQUBE_DB_PASSWORD="your_secure_password"

Usage

Production Deployment

Run the Ansible playbook to install PostgreSQL and SonarQube on the production server:

ansible-playbook playbooks/main.yml

Local Development with Vagrant

  1. Start the Vagrant VM:
vagrant up
  1. Run the Ansible playbook against the Vagrant VM:
ansible-playbook -i inventories/dev/hosts playbooks/main.yml

Alternatively, you can provision during VM creation:

ANSIBLE_PROVISION=true vagrant up
  1. Access SonarQube in your browser at:
http://localhost:9000
  1. To stop the VM:
vagrant halt
  1. To remove the VM:
vagrant destroy

Accessing SonarQube

After successful installation, SonarQube will be available at:

Default credentials:

Customization

You can customize the installation by modifying the variables in:

The project has been simplified to reduce duplication and make maintenance easier. Role-specific variables are kept in their respective role defaults to ensure they're always available when the role is executed, while common variables are centralized in group_vars/all.yml.

Vagrant VM Specifications

The local development VM is configured with:

Testing

This project includes comprehensive testing mechanisms to ensure proper functionality. You can run all tests using the provided helper script or execute specific tests individually.

Running All Tests

Use the simplified test runner script:

# For Vagrant environment
python tests/run_tests.py --vagrant

# For production environment
python tests/run_tests.py --host=<your-server-ip>

# To skip integration tests
python tests/run_tests.py --vagrant --skip-integration

This will run:

  1. Syntax and linting checks (combined in a single step)
  2. Integration tests (if not skipped)

Running Individual Tests

Syntax and Linting Tests

# Run syntax checks and linting
ansible-playbook tests/syntax_check.yml

# Run only ansible-lint
ansible-lint

Integration Tests

After deployment, verify the SonarQube installation:

# For production
./tests/integration_test.sh <server-ip> 9000

# For local Vagrant VM
./tests/integration_test.sh localhost 9000

Molecule Tests

Test individual roles in isolation:

# Test PostgreSQL role
cd roles/postgresql
molecule test

# Test SonarQube role
cd roles/sonarqube
molecule test

Idempotency Testing

Verify that running the playbook multiple times doesn't cause changes:

# First run
ansible-playbook -i inventories/dev/hosts playbooks/main.yml

# Second run (should report no changes)
ansible-playbook -i inventories/dev/hosts playbooks/main.yml

Test Configuration

Prerequisites for Testing

  1. Install test dependencies:
pip install molecule molecule-plugins[vagrant] yamllint ansible-lint
  1. Ensure VirtualBox and Vagrant are installed for Molecule tests:
# Ubuntu/Debian
sudo apt-get install virtualbox vagrant